package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.dao.impl
 * @Description:
 * @Date 2022/5/21 15:26
 */
public class UserDaoImpl implements UserDao {

    //获取连接对象
    private QueryRunner runner = new QueryRunner(DBUtil.getDateSource());
    @Override
    public User findByUserName(String username) {
        String sql = "select * from tb_user where username = ?";
        try {
            User user = runner.query(sql, new BeanHandler<>(User.class), username);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into tb_user(username , password , email , gender , status , role , code) " +
                "value( ? , ? , ? , ? , ? ,? , ?) )";
        try {
            int count = runner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(),
                    user.getStatus(), user.getRole(), user.getCode());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
